<template>
    <div class="dashboard-container">
      <div class="app-container">
        <div class="edit-form">
          <el-form ref="userForm" :model="userInfo" :rules="rules" label-width="220px">
            <!-- 姓名 -->
            <el-row>
              <el-col :span="12">
                <el-form-item label="姓名" prop="username">
                  <el-input v-model="userInfo.username" size="mini" class="inputW" />
                </el-form-item>
              </el-col>
  
            </el-row>
            <!-- 工号  -->
            <el-row>
              <el-col :span="12">
                <el-form-item label="工号" prop="workNumber">
                  <!-- 工号是系统生成的                   disable禁用这个组件         -->
                  <el-input v-model="userInfo.workNumber" disabled size="mini" class="inputW" />
                </el-form-item>
              </el-col>
            </el-row>
            <!--手机  -->
            <el-row>
              <el-col :span="12">
                <el-form-item :disabled="!!$route.params.id" label="手机" prop="mobile">
                  <el-input
                    v-model="userInfo.mobile"
                    :disabled="!!$route.params.id"
                    size="mini"
                    class="inputW"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 部门 -->
            <el-row>
              <el-col :span="12">
                <el-form-item label="部门" prop="departmentId">
                  <!-- v-model 是 Vue.js 中用于实现双向数据绑定的指令。在这里，它将组件的 value 属性与 userInfo.departmentId 绑定。
当选择框的值发生变化时，userInfo.departmentId 将自动更新，反之亦然。 -->
                  <select-tree v-model="userInfo.departmentId" size="mini" class="inputW" />
                <!--放置及联部门组件 会单独封装  -->
                <!-- inputW样式会给到selectree中 -->
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item   label="聘用形式" prop="formOfEmployment">
                  <el-select v-model="userInfo.formOfEmployment" size="mini" class="inputW">
                  <el-option label="正式" :value="1"></el-option>
                  <el-option label="非正式" :value="2"></el-option>
                </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="入职时间" prop="timeOfEntry">
                  <el-date-picker
                    size="mini"
                    type="date"
                    value-format="yyyy-MM-dd"
                    class="inputW"
                    v-model="userInfo.timeOfEntry"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="转正时间" prop="correctionTime">
                  <el-date-picker
                  v-model="userInfo.correctionTime"
                    size="mini"
                    type="date"
                    class="inputW"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
          <el-form-item label="员工头像">
         <!-- 放置头像 -->
          <image-upload v-model="userInfo.staffPhoto"></image-upload>
            </el-form-item>
            </el-col>
          </el-row>
            <!-- 保存个人信息 -->
            <el-row type="flex">
              <el-col :span="12" style="margin-left:220px">
                <el-button size="mini" type="primary" @click="saveData" >保存更新</el-button>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </div>
    </div>
  </template>
  <script>
  import SelectTree from './select-tree.vue'
  import ImageUpload from './components/image-upload.vue'
  import {addEmployee, getEmployeeDetail,updataEmployee} from '@/api/employee'
  export default {
  components: {SelectTree ,ImageUpload},
  data() {
    return {
      userInfo: {
        username: '', // 用户名
        mobile: '', // 手机号
        workNumber: '', // 工号
        formOfEmployment: null, // 聘用形式
        departmentId: null, // 部门id
        timeOfEntry: '', // 入职时间
        correctionTime: '' ,// 转正时间
        staffPhoto:'https://img.wxcha.com/m00/e2/d8/7b715d6aeee61f9cf108b10feec0a73f.jpg'
      },
      rules: {
        username: [{ required: true, message: '请输入姓名', trigger: 'blur' }, {
          min: 1, max: 4, message: '姓名为1-4位'
        }],
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }, {
        //   pattern 正则表达式
          pattern: /^1[3-9]\d{9}$/,
          message: '手机号格式不正确',
          trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '请选择聘用形式', trigger: 'blur' }],
        departmentId: [{ required: true, message: '请选择部门', trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '请选择入职时间', trigger: 'blur' }],
        correctionTime: [{ required: true, message: '请选择转正时间', trigger: 'blur' }, {
          validator: (rule, value, callback) => {
            if (this.userInfo.timeOfEntry) {
              // 这里使用 new Date() 将 timeOfEntry 和 value 转换为日期对象，然后进行比较。
              if (new Date(this.userInfo.timeOfEntry) > new Date(value)) {
                callback(new Error('转正时间不能小于入职时间'))
                return  //使用 return 语句结束当前函数，避免继续执行后面的代码。
              }
            }
            callback()
          }
        }]
      }
}
    },methods:{
     async getEmployeeDetail(){
    this.userInfo=await getEmployeeDetail(this.$route.params.id)
      },
      saveData() {
  this.$refs.userForm.validate(async isOK => {
    if (isOK) {
      //判断编辑模式
if(this.$route.params.id){
  await updataEmployee(this.userInfo)
  this.$message.success('更新员工成功')
}else{
  await addEmployee(this.userInfo)
        this.$message.success('新增员工成功')
   }   
     this.$router.push('/employee')
   
    }
  })

}
    },created(){
      //如何获取录音参数中的id
    // if(this.$route.params.id) {this.getEmployeeDetail()}
    this.$route.params.id&&this.getEmployeeDetail()

    }
  }
  </script>
  
    <style scoped lang="scss">
    .edit-form {
      background: #fff;
      padding: 20px;
      .inputW {
        width: 380px
      }
    }
  
    </style>
  
  